import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app_05/utils/ScreenAdapter.dart';
import 'package:flutter_app_05/widget/bottom_navigation_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'indicator.dart';

class VviewPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _VviewPageState();
}

class _VviewPageState extends State<VviewPage> {

  static PageController controller = PageController(
    initialPage: 0,
  );

  void _pageChanged(int index) {
    setState(() {});
  }

  final List<Widget> _pages = <Widget>[
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new Image(
        image: new AssetImage("images/start_1.png"),
      ),
    ),
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new Image(
        image: new AssetImage("images/start_2.png"),
      ),
    ),
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new Image(
        image: new AssetImage("images/start_3.png"),
      ),
    ),
    new ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: new Image(
        image: new AssetImage("images/start_4.png"),
      ),
    ),
  ];

  Widget jinru() {
    return new Container(
      decoration: new BoxDecoration(
        image: new DecorationImage(
          image: new AssetImage("images/start_5.png"),
        ),
      ),
      alignment: Alignment.center,
      padding: EdgeInsets.only(top: ScreenAdapter.height(780)),
      child: FlatButton(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(80.0)),
        ),
        child: Text(
          '进入',
          style: TextStyle(color: Colors.white, fontSize: 15),
        ),
        color: Colors.green,
        onPressed: () async {
          SharedPreferences prefs = await SharedPreferences.getInstance();
          prefs.setBool("isCanInto", true);
          Navigator.of(context).pushReplacement(MaterialPageRoute(
              builder: (context) => BottomNavigationWidget()));
        },
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
      return Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
              child: SizedBox(
                  height: ScreenAdapter.height(1334),
                  child: Container(
                    color: Colors.white,
                    child: PageView.builder(
                      onPageChanged: _pageChanged,
                      controller: controller,
                      itemBuilder: (context, index) {
                        print("aaaaaaaaaaaa" + index.toString());
                        if (index < 4) {
                          return _pages[index];
                        } else if (index == 4) {
                          return jinru();
                        }
                      },
                    ),
                  ))),
          Positioned(
              bottom: ScreenAdapter.height(180),
              child: Center(
                child: Indicator(
                  controller: controller,
                  itemCount: 5,
                ),
              ))
        ],
      );
  }
}
